import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:safe_manage/config/my_app_provider.dart';
import 'package:safe_manage/pages/alarm/alarm_page/alarm_page.dart';
import 'package:safe_manage/pages/gallery/gallery_camera/gallery_camera_page/gallery_camera_page.dart';
import 'package:safe_manage/pages/home/home_page/home_page.dart';
import 'package:safe_manage/pages/my/my_page/my_page.dart';
import 'base_tab_bar_controller.dart';
import 'package:badges/badges.dart' as badges;
import 'package:safe_manage/pages/alarm/alarm_page/alarm_controller.dart';
import 'package:safe_manage/pages/gallery/gallery_camera/gallery_camera_page/gallery_camera_controller.dart';
import 'package:safe_manage/pages/home/home_page/home_controller.dart';
import 'package:safe_manage/pages/my/my_page/my_controller.dart';

class BaseTabBarPage extends StatelessWidget {
  final controller = Get.find<BaseTabBarController>();
  BaseTabBarPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _buildBottomNavigationBar(context),
      body: _buildPageView(),
    );
  }

  Widget _buildBottomNavigationBar(BuildContext context) {
    return Obx(
      () => BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              "images/tabbar/home.png",
              width: 24,
              height: 24,
            ),
            activeIcon: Image.asset(
              "images/tabbar/home_select.png",
              width: 24,
              height: 24,
            ),
            label: '控制台',
          ),
          BottomNavigationBarItem(
            icon: badges.Badge(
              position: badges.BadgePosition.topEnd(top: -5, end: -15),
              showBadge: context.watch<MyAppProvider>().notifyCount != 0,
              ignorePointer: false,
              badgeStyle: badges.BadgeStyle(
                shape: badges.BadgeShape.square,
                badgeColor: Colors.red,
                padding: const EdgeInsets.fromLTRB(5, 3, 5, 3),
                borderRadius: BorderRadius.circular(10),
                // borderSide: BorderSide(color: Colors.white, width: 2),
                // borderGradient: badges.BadgeGradient.linear(
                //     colors: [Colors.red, Colors.black]),
                // badgeGradient: badges.BadgeGradient.linear(
                //   colors: [Colors.blue, Colors.yellow],
                //   begin: Alignment.topCenter,
                //   end: Alignment.bottomCenter,
                // ),
                elevation: 10,
              ),
              badgeContent: Text(
                context.watch<MyAppProvider>().notifyCount > 99
                    ? "99+"
                    : context.watch<MyAppProvider>().notifyCount.toString(),
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 10,
                  height: 1,
                ),
              ),
              child: Image.asset(
                "images/tabbar/alarm.png",
                width: 24,
                height: 24,
              ),
            ),
            activeIcon: badges.Badge(
              position: badges.BadgePosition.topEnd(top: -5, end: -15),
              showBadge: context.watch<MyAppProvider>().notifyCount != 0,
              ignorePointer: false,
              badgeStyle: badges.BadgeStyle(
                shape: badges.BadgeShape.square,
                badgeColor: Colors.red,
                padding: const EdgeInsets.fromLTRB(5, 3, 5, 3),
                borderRadius: BorderRadius.circular(10),
                // borderSide: BorderSide(color: Colors.white, width: 2),
                // borderGradient: badges.BadgeGradient.linear(
                //     colors: [Colors.red, Colors.black]),
                // badgeGradient: badges.BadgeGradient.linear(
                //   colors: [Colors.blue, Colors.yellow],
                //   begin: Alignment.topCenter,
                //   end: Alignment.bottomCenter,
                // ),
                elevation: 10,
              ),
              badgeContent: Text(
                context.watch<MyAppProvider>().notifyCount > 99
                    ? "99+"
                    : context.watch<MyAppProvider>().notifyCount.toString(),
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 10,
                  height: 1,
                ),
              ),
              child: Image.asset(
                "images/tabbar/alarm_select.png",
                width: 24,
                height: 24,
              ),
            ),
            label: '报警事件',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              "images/tabbar/gallery.png",
              width: 24,
              height: 24,
            ),
            activeIcon: Image.asset(
              "images/tabbar/gallery_select.png",
              width: 24,
              height: 24,
            ),
            label: '工作台',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              "images/tabbar/my.png",
              width: 24,
              height: 24,
            ),
            activeIcon: Image.asset(
              "images/tabbar/my_select.png",
              width: 24,
              height: 24,
            ),
            label: '我的',
          ),
        ],
        currentIndex: controller.currentPage.value,
        type: BottomNavigationBarType.fixed,
        backgroundColor: const Color(0xfff7f7f7),
        selectedFontSize: 12,
        unselectedFontSize: 12,
        onTap: (int index) => controller.switchBottomTabBar(index),
      ),
    );
  }

  /// 内容页
  Widget _buildPageView() {
    Get.put(HomeController());
    Get.put(AlarmController());
    Get.put(GalleryCameraController());
    Get.put(MyController());
    var pageList = [
      HomePage(),
      const AlarmPage(),
      GalleryCameraPage(),
      MyPage(),
    ];
    return Column(
      children: [
        Expanded(
          child: Obx(
            () => IndexedStack(
              index: controller.currentPage.value,
              children: pageList,
            ),
          ),
          // child: PageView(
          //   //禁止滑动
          //   physics: const NeverScrollableScrollPhysics(),
          //   controller: controller.pageController,
          //   onPageChanged: (index) => controller.onPageChanged(index),
          //   children: pageList,
          // ),
        ),
        Container(
          height: 0.5,
          width: Get.width,
          color: Colors.black26,
        ),
      ],
    );
  }
}
